/* 
    Purpose: Using the output from 1a_Census1960_fathers30to50.do,
    		 this file calculates average income at various levels of
    		 variation, separately for farmers and laborers. The ratio
    		 of farmer to laborer average income (by level of variation)
    		 is then calculated.

    Note: The 1960 5% Census is big enough that it is not 
    	  necessary to weight averages. Perwt confirms that 
    	  everyone is assigned the same weight.

    Creates: All output files have the prefix "FarmerRatios_1960_"
*/
clear
set more off
cd "$Mydirectory1/1_DataSources/CensusData/"

*------------------------------------------------------------------------------------*
*------------------------------------------------------------------------------------*

	use ./input/Census1960_5pct_fathers30to50_adjustments.dta, clear 
	sum perwt, d 

* Keep farmers and laborers
	gen farmer = occ1950==100 | occ1950==123
	gen laborer = occ1950==810 | occ1950==820 | occ1950==830 | occ1950==840 
	
	keep if farmer==1 | laborer==1
	
* Count # of farmers and # of laborers at each level of variation
	egen ncell_L_all=total(laborer)
	egen ncell_L_race=total(laborer), by(race)
	egen ncell_L_race_south=total(laborer), by(race south_merge)
	egen ncell_L_race_south_edu=total(laborer), by(race south_merge edu)
	egen ncell_L_race_region=total(laborer), by(race region_merge)
	egen ncell_L_race_region_edu=total(laborer), by(race region_merge edu)

	egen ncell_F_all=total(farmer)
	egen ncell_F_race=total(farmer), by(race)
	egen ncell_F_race_south=total(farmer), by(race south_merge) 
	egen ncell_F_race_south_edu=total(farmer), by(race south_merge edu) 
	egen ncell_F_race_region=total(farmer), by(race region_merge)
	egen ncell_F_race_region_edu=total(farmer), by(race region_merge edu)
	
*------------------------------------------------------------------------------------*
*------------------------------------------------------------------------------------*

************************************
*** AVERAGE INCOME: FARM LABORERS
************************************
	tempfile fulldata
	save `fulldata'

foreach inc in person hh {

	use `fulldata', clear

	local number_obs "20"
	
	if "`inc'"=="person" local income "inctot"
	if "`inc'"=="fam" local income "ftotinc"
	if "`inc'"=="hh" local income "hh_income" 
	
	local measure "mean"
	
* 0. Overall 
	egen laborers_income_all_aux = `measure'(`income') if laborer==1
	egen laborers_income_all = mean(laborers_income_all_aux)
	
* 1. By race
	egen laborers_income_aux=`measure'(`income') if laborer==1, by(race)
	egen laborers_income=mean(laborers_income_aux), by(race)

	//Note: No cell is too small at this level
	tab ncell_L_race 
	tab ncell_F_race
	
* 2. Race x south
	egen laborers_income_south_aux=`measure'(`income') if laborer==1, by(race south_merge)
	egen laborers_income_south=mean(laborers_income_south_aux), by(race south_merge)
	
	tab ncell_L_race_south
	tab ncell_F_race_south
	tab race south_merge if (ncell_L_race_south<`number_obs' | ncell_F_race_south<`number_obs')

	/*Note: If a cell has too few observations (i.e., <20)
	        the average national income by race for laborers will 
	        be assigned to the cell. */	
	replace laborers_income_south = laborers_income if (ncell_L_race_south<`number_obs' | ncell_F_race_south<`number_obs')
	
* 3. Race x south x education
	egen laborers_income_south_edu_aux=`measure'(`income') if laborer==1, by(race south_merge edu)
	egen laborers_income_south_edu=mean(laborers_income_south_edu_aux), by(race south_merge edu)
	
	tab ncell_L_race_south_edu
	tab ncell_F_race_south_edu
	bysort south_merge: tab race edu if (ncell_L_race_south_edu<`number_obs' | ncell_F_race_south_edu<`number_obs')

	/*Note: If a cell has too few observations (i.e., <20)
	        the average national income by race and south
	        for laborers will be assigned to the cell. */
	replace laborers_income_south_edu = laborers_income_south if (ncell_L_race_south_edu<`number_obs' | ncell_F_race_south_edu<`number_obs')
	
* 4. Race x region 
	egen laborers_income_reg_aux=`measure'(`income') if laborer==1, by(race region_merge)
	egen laborers_income_region=mean(laborers_income_reg_aux), by(race region_merge)
	
	tab ncell_L_race_region
	tab ncell_F_race_region
	tab race region_merge if (ncell_L_race_region<`number_obs' | ncell_F_race_region<`number_obs')

	/*Note: If a cell has too few observations (i.e., <20)
	        the average national income by race for laborers will 
	        be assigned to the cell. */	
	replace laborers_income_region = laborers_income if (ncell_L_race_region<`number_obs' | ncell_F_race_region<`number_obs')
	
* 5. Race x region x edu
	egen laborers_income_reg_edu_aux=`measure'(`income') if laborer==1, by(race region_merge edu)
	egen laborers_income_region_edu=mean(laborers_income_reg_edu_aux), by(race region_merge edu)
	
	tab ncell_L_race_region_edu
	tab ncell_F_race_region_edu
	bysort region_merge: tab race edu if (ncell_L_race_region_edu<`number_obs' | ncell_F_race_region_edu<`number_obs')

	/*Note: If a cell has too few observations (i.e., <20)
	        the average national income by race and region
	        for laborers will be assigned to the cell. */
	replace laborers_income_region_edu = laborers_income_region if (ncell_L_race_region_edu<`number_obs' | ncell_F_race_region_edu<`number_obs')
	
    //Account for in-kind income of laborers 
        /*Note: See https://data.nber.org/data-appendix/w23395/Appendix_May_2020.pdf
        		(page 11) for more info */
	foreach x in laborers_income_all laborers_income laborers_income_south laborers_income_south_edu laborers_income_region laborers_income_region_edu {
		replace `x' = `x'*1.19
	}

	drop *_aux
	
*------------------------------------------------------------------------------------*
*------------------------------------------------------------------------------------*
	
*******************************
*** AVERAGE INCOME: FARMERS
*******************************

* 0. Overall 
	egen farmers_income_all_aux = `measure'(`income') if farmer==1
	egen farmers_income_all = mean(farmers_income_all_aux)

* 1. By race
	egen farmers_income_aux=`measure'(`income') if farmer==1, by(race)
	egen farmers_income=mean(farmers_income_aux), by(race)

	//Note: No cell is too small at this level	
	tab ncell_L_race 
	tab ncell_F_race

* 2. Race x south
	egen farmers_income_south_aux=`measure'(`income') if farmer==1, by(race south_merge)
	egen farmers_income_south=mean(farmers_income_south_aux), by(race south_merge)
	
	tab ncell_L_race_south
	tab ncell_F_race_south
	tab race south_merge if (ncell_L_race_south<`number_obs' | ncell_F_race_south<`number_obs')

	/*Note: If a cell has too few observations (i.e., <20)
	        the average national income by race for farmers will 
	        be assigned to the cell. */	
	replace farmers_income_south = farmers_income if (ncell_L_race_south<`number_obs' | ncell_F_race_south<`number_obs')
	
* 3. Race x south x education
	egen farmers_income_south_edu_aux=`measure'(`income') if farmer==1, by(race south_merge edu)
	egen farmers_income_south_edu=mean(farmers_income_south_edu_aux), by(race south_merge edu)
	
	tab ncell_L_race_south_edu
	tab ncell_F_race_south_edu
	bysort south_merge: tab race edu if (ncell_L_race_south_edu<`number_obs' | ncell_F_race_south_edu<`number_obs')

	/*Note: If a cell has too few observations (i.e., <20)
	        the average national income by race and south 
	        for farmers will be assigned to the cell. */
	replace farmers_income_south_edu = farmers_income_south if (ncell_L_race_south_edu<`number_obs' | ncell_F_race_south_edu<`number_obs')
	
* 4. Race x region
	egen farmers_income_reg_aux=`measure'(`income') if farmer==1, by(race region_merge)
	egen farmers_income_region=mean(farmers_income_reg_aux), by(race region_merge)
	
	tab ncell_L_race_region
	tab ncell_F_race_region
	tab race region_merge if (ncell_L_race_region<`number_obs' | ncell_F_race_region<`number_obs')

	/*Note: If a cell has too few observations (i.e., <20)
	        the average national income by race for farmers will 
	        be assigned to the cell. */	
	replace farmers_income_region = farmers_income if (ncell_L_race_region<`number_obs' | ncell_F_race_region<`number_obs')
	
* 5. Race x region x education	
	egen farmers_income_reg_edu_aux=`measure'(`income') if farmer==1, by(race region_merge edu)
	egen farmers_income_region_edu=mean(farmers_income_reg_edu_aux), by(race region_merge edu)
	
	tab ncell_L_race_region_edu
	tab ncell_F_race_region_edu
	bysort region_merge: tab race edu if (ncell_L_race_region_edu<`number_obs' | ncell_F_race_region_edu<`number_obs')
	
	/*Note: If a cell has too few observations (i.e., <20)
	        the average national income by race and region
	        for farmers will be assigned to the cell. */	
	replace farmers_income_region_edu = farmers_income_region if (ncell_L_race_region_edu<`number_obs' | ncell_F_race_region_edu<`number_obs')
	

* Account for in-kind income of farmers
    /*Note: See https://data.nber.org/data-appendix/w23395/Appendix_May_2020.pdf
        		(page 12) for more info */
	foreach x in farmers_income_all farmers_income farmers_income_south farmers_income_south_edu farmers_income_region farmers_income_region_edu {
		replace `x' = `x'*1.35
	}
	
	drop *_aux
	
*------------------------------------------------------------------------------------*
*------------------------------------------------------------------------------------*
	
************************************************
*** RATIOS OF FARMER TO LABORER AVG. INCOME
************************************************
	gen ratio_farm_`inc'_all = farmers_income_all / laborers_income_all
	gen ratio_farm_`inc' = farmers_income / laborers_income
	gen ratio_farm_south_`inc' = farmers_income_south / laborers_income_south
	gen ratio_farm_south_edu_`inc' = farmers_income_south_edu / laborers_income_south_edu
	gen ratio_farm_region_`inc' = farmers_income_region / laborers_income_region
	gen ratio_farm_region_edu_`inc' = farmers_income_region_edu / laborers_income_region_edu

	keep race edu region_merge south_merge ratio* serial pernum
	
	label var ratio_farm_`inc'_all "Ratio of farmer to farm laborer (`inc') average income, all"
	label var ratio_farm_`inc' "Ratio of farmer to farm laborer (`inc') average income, by race"
	label var ratio_farm_south_`inc' "Ratio of farmer to farm laborer (`inc') average income, by race x south"
	label var ratio_farm_south_edu_`inc'  "Ratio of farmer to farm laborer (`inc') average income, by race x south x edu"
	label var ratio_farm_region_`inc' "Ratio of farmer to farm laborer (`inc') average income, by race x region"
	label var ratio_farm_region_edu_`inc'  "Ratio of farmer to farm laborer (`inc') average income, by race x region x edu"
	
	tempfile income_`inc'
	save `income_`inc''
	
}

	use `income_person', clear
	merge 1:1 serial pernum using `income_hh'
	drop _merge
	
*------------------------------------------------------------------------------------*
*------------------------------------------------------------------------------------*

*********************
* SAVE (BY LEVEL)
*********************
	compress
	
* Overall
	preserve
	keep if _n==1
	gen number=1
	keep number ratio_farm_person_all ratio_farm_hh_all
	
	save "./output/FarmerRatios_1960_all.dta", replace
	restore

* By race
	preserve
	bysort race: keep if _n==1
	keep race ratio_farm_person ratio_farm_hh
	
	save "./output/FarmerRatios_1960_byrace.dta", replace
	restore

* Race x south
	preserve
	bysort race south_merge: keep if _n==1
	keep race south_merge ratio_farm_south_hh ratio_farm_south_person
	
	save "./output/FarmerRatios_1960_byrace_bysouth.dta", replace
	restore

* Race x south (version for Jácome et al survey respondents)
	preserve
	bysort race south_merge: keep if _n==1
	keep race south_merge ratio_farm_south_hh ratio_farm_south_person
	
	rename south_merge south_merge_son

	save "./output/FarmerRatios_1960_byrace_bysouth_R.dta", replace
	restore

* Race x south x education
	preserve
	bysort race south_merge edu: keep if _n==1
	keep race south_merge edu ratio_farm_south_edu_hh ratio_farm_south_edu_person
	
	save "./output/FarmerRatios_1960_byrace_bysouth_byedu.dta", replace
	restore

* Race x region
	preserve
	bysort race region_merge: keep if _n==1
	keep race region_merge ratio_farm_region_hh ratio_farm_region_person
	
	save "./output/FarmerRatios_1960_byrace_byregion.dta", replace
	restore

* Race x region x education
	preserve
	bysort race region_merge edu: keep if _n==1
	keep race region_merge edu ratio_farm_region_edu_hh ratio_farm_region_edu_person
	
	save "./output/FarmerRatios_1960_byrace_byregion_byedu.dta", replace
	restore

